package com.dcivision.framework.taglib.channel;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import com.dcivision.framework.TextUtility;
import com.dcivision.framework.taglib.layout.PagerTag;

public abstract class AbstractAjaxPager {

  protected PageContext pageContext = null;
  protected PagerTag pagerTag = null;
  protected int pageSize = 0;
  protected List recordList = null;
  
  public String[] getPageIndexContext(PageContext pageContext,int pageSize,List recordList,String pattern) throws Exception{
    this.pageContext = pageContext;
    this.pageSize = pageSize;
    this.recordList = recordList;
    
    String[] pageInfoAndPageIndex = new String[2];
    this.initPagerTag();
    this.pagerTagDoStratTag();
    String pageIndexStr = this.getPageIndex();
    String pageInfoStr = this.getPageInfo(pattern);
    pageInfoAndPageIndex[0] = pageInfoStr;
    pageInfoAndPageIndex[1] = pageIndexStr;
    return pageInfoAndPageIndex;
  }
  
  protected abstract String getPageInfo(String pattern) throws Exception;
  protected abstract String getPageIndex()throws Exception;
  
  protected void initPagerTag(){
    pagerTag = new PagerTag();
    pagerTag.setPageContext(pageContext);
    pageContext.getRequest().setAttribute("recordList", recordList);
    pagerTag.setName("recordList");
  }
  
  protected void pagerTagDoStratTag(){
    //pagerTag.setM_nMaxPageItems(TextUtility.parseInteger(SystemParameterFactory.getSystemParameter(SystemParameterConstant.PAGE_OFF_SET)));
    pagerTag.setM_nMaxPageItems(this.pageSize);
    //--should changed-------5,8,10....
    pagerTag.setMaxIndexPages(TextUtility.parseInteger("10"));
    //--should changed-------10

    if (pagerTag.getM_sUrl() == null) {
      pagerTag.setM_sUrl(((HttpServletRequest)pageContext.getRequest()).getRequestURI());
    }
    pagerTag.setM_nPageNumber(pagerTag.pageNumber(pagerTag.getM_nOffset()));
    pagerTag.setM_nPageNumberInteger(new Integer(1+pagerTag.getM_nPageNumber()));
   // pageContext.setAttribute(id+PAGE_NUMBER, m_nPageNumberInteger);
   // pageContext.setAttribute(id+m_nOffset, new Integer(m_nOffset));
   // pageContext.setAttribute(id+MAX_ITEMS, new Integer(m_nMaxItems));
  }
  
  protected String pagerGotoPage(String pageURL){
    StringBuffer bstr = new StringBuffer();
    bstr.append("channel"+pageContext.getRequest().getAttribute(AjaxConstant.FILTERNAME)+".refreshChannelWithParameters(");
    bstr.append(pageURL);
    bstr.append(",null)");
    return bstr.toString();
  }
}
